package com.avocado.configuration.validator;

import com.avocado.configuration.repository.DataDictionaryRepository;
import lombok.extern.slf4j.Slf4j;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class DataDictionaryAddKeyValidator implements ConstraintValidator<DataDictionaryAddKeyValidation, String> {


    private final DataDictionaryRepository dataDictionaryRepository;

    public DataDictionaryAddKeyValidator(DataDictionaryRepository dataDictionaryRepository) {
        this.dataDictionaryRepository = dataDictionaryRepository;
    }

    @Override
    public void initialize(DataDictionaryAddKeyValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {


        try {

            long count=dataDictionaryRepository.getCountByKey(value);

            return count<1L;

        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage());
        }
        return false;
    }
}
